<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <el-button type="primary" @click="addReward">添加</el-button>
      <el-input style="width: 30%;margin: 0 5px" v-model="searchParams.rewardName" placeholder="请输入名称"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="getAllReward">搜索</el-button>
    </div>
    <el-table
        :data="rewards"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="rewardName" label="奖励名"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.userType!=='0'">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"  v-if="scope.row.userType!=='0'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
    <reward-dialog :visible.sync="addDialog" title="添加奖励" @addResult="getAllReward"></reward-dialog>
    <reward-update-dialog :visible.sync="updateDialog"  :reward="updateReward" @addResult="getAllReward"></reward-update-dialog>
  </div>
</template>

<script>
import {deleteReward, getRewardByPage} from "@/api/reward";
import RewardDialog from "@/components/dialog/rewardDialog";
import RewardUpdateDialog from "@/components/dialog/rewardUpdateDialog";

export default {
  name: "index",
  components: {RewardUpdateDialog, RewardDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        rewardName:'',
        rewardType:''
      },
      rewards:[],
      total:10,
      addDialog:false,
      updateReward:{},
      updateDialog:false
    }
  },
  methods:{
    getAllReward(){
      getRewardByPage(this.searchParams).then(res=>{
        if (res.code===200){
          this.$message.success("奖励数据获取成功")
          this.rewards=res.data.records;
          this.total=res.data.total;
        }
      })
    },
    handleSizeChange(val){
      this.getAllReward();
    },
    handleCurrentChange(val){
      this.getAllReward();
    },
    handleEdit(index,row){
      this.updateReward=JSON.parse(JSON.stringify(row));
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteReward(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功删除")
          this.getAllReward();
        }
      })
    },
    addReward(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllReward();
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
    margin: 16px;
  }
}
</style>
